package com.hailou.dao;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.hailou.util.DateTimeUtile;

/**
 * @author hailou
 * @date 2013-12-9
 * @version 1.0
 */
public class ContentDao extends ConnectionDB {
	
	/***************向内容表中插入数据**************************/
	public static void insertIntoContent(Map<String,String> params){
		 
		ConnectionDB db = new ConnectionDB();
		 
		StringBuilder sb = new StringBuilder();
		sb.append("insert into t_content (");
		
		Set<Entry<String,String>> set = params.entrySet();
		for(Iterator<Entry<String,String>> iterator = set.iterator();iterator.hasNext();){
			Map.Entry<String, String> entry= iterator.next();
			sb.append(entry.getKey()).append(",");
		}
		sb.deleteCharAt(sb.length()-1).append(") values (");
		for(Iterator<Entry<String,String>> iterator = set.iterator();iterator.hasNext();){
			Map.Entry<String, String> entry= iterator.next();
			sb.append("'").append(entry.getValue()).append("',");
		}
		sb.deleteCharAt(sb.length()-1).append(")").toString();
		int flag = 0;
		flag = db.executeUpdate(sb.toString(),null);
		if(flag == 0 ){
			System.out.println("===内容插入失败===");
		}
	} 
	/***************返回用户最后一次输入的数据内容**************************/
	public static String getMaxContentByUserId(String userid,String whereSql){
		ConnectionDB db = new ConnectionDB();
		StringBuilder sb = new StringBuilder();
		if(whereSql!=null){
			sb.append(" and ").append(whereSql);
		}else{
			sb.append("");
		}
		String sql = "select o.id,o.content,o.ctype,o.ctime,o.touserid,o.userid from t_content o where o.userid =? "+sb.toString()+" and o.ctype='text' order by o.ctime desc";
		System.out.println(sql);
		
		List<Object> contentList= db.excuteQuery(sql, new String[]{userid});
		Map<String,String> maxContent= (Map<String,String>)contentList.get(0);
		String content = maxContent.get("content");
		
		return content;
	}
	
	/**************** 将用户的回复信息插入数据库 ***********************/
	public static void insertIntoContent(String content,String msgType,String toUserName,String fromUserName){
		
		Map<String, String> params = new LinkedHashMap<String, String>();
		if(content!=null||!"".equals(content)){
			params.put("content", content);
		}
		if(msgType!=null||!"".equals(msgType)){
			params.put("ctype", msgType);
		}
		if(toUserName!=null||!"".equals(toUserName)){
			params.put("touserid", toUserName);
		}
		if(fromUserName!=null||!"".equals(fromUserName)){
			params.put("userid", fromUserName);
		}
		params.put("ctime", DateTimeUtile.getNowTime());
		/********入库*****/
		insertIntoContent(params);
	}
	
	public static void main(String[] args) {
		Map<String, String> map = new LinkedHashMap<String, String>();
		map.put("content", "aa");
		map.put("ctype", "text");
		map.put("ctime", "aa11");
		map.put("touserid", "11");
		map.put("userid", "22");
		
		insertIntoContent(map);
	}
}
